<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="~@/assets/images/logo.svg" alt="" />
      <span v-if="!isCollapse" class="title">Vue3 + TS</span>
    </div>
    <el-menu
      :default-active="defaultActive"
      class="el-menu-vertical-demo"
      unique-opened
      background-color="#001529"
      text-color="#fff"
      :collapse="isCollapse"
    >
      <el-submenu
        v-for="(item, index) in menuArray"
        :index="index + ''"
        :key="index + item.name"
      >
        <template #title>
          <i :class="item.icon"></i>
          <span>{{ item.name }}</span>
        </template>
        <el-menu-item
          v-for="(item2, index2) in item.children"
          :index="index + '-' + index2"
          :key="index2 + item.name"
          @click="handleRoute(index + '-' + index2, item2.url)"
          class="animate__animated animate__rotateIn"
        >
          {{ item2.name }}</el-menu-item
        >
      </el-submenu>
    </el-menu>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { menuArr } from '@/assets/common'
import { useRouter } from 'vue-router'

export default defineComponent({
  name: 'Menu',
  props: {
    isCollapse: {
      type: Boolean,
      default: false
    }
  },
  setup() {
    const Active = sessionStorage.getItem('defaultActive')
    const defaultActive = ref('1')
    if (Active) {
      defaultActive.value = Active
    }
    const router = useRouter()
    const handleRoute = (index: string, url: string) => {
      defaultActive.value = index
      router.push(url)
      sessionStorage.setItem('defaultActive', defaultActive.value)
    }
    const menuArray = reactive(menuArr)
    return {
      menuArray,
      defaultActive,
      handleRoute
    }
  }
})
</script>

<style scoped lang="less">
.iConWidth {
  line-height: 14px;
}
.logo {
  display: flex;
  height: 28px;
  padding: 12px 10px 8px 10px;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;

  .img {
    height: 100%;
    margin: 0 10px;
  }

  .title {
    font-size: 16px;
    font-weight: 700;
    color: white;
  }
}
/deep/.el-menu {
  border-right: none;
}
</style>
